<?php 
error_reporting(E_ALL);
session_start();
require_once 'application/library/Betting_lib.php';

$bet = new Betting_lib();

if(isset($_SESSION['userid'])){
	$button = '<input type="button" name="submit_bid" value="Bid now" class="bidnow">';
} else {
	$button = '<input type="button" name="login" value="Bid now" onClick="location.href=\'login.php\'">';
}
?>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
		<title>2Bet  <?php echo $_SESSION['username']; ?></title>
		<script src="js/jquery-1.9.1.min.js"></script>
		<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
		<link href="css/main.css" media="all" rel="stylesheet" type="text/css" />
		
		<link rel="stylesheet" type="text/css" href="css/style.css" />
		<script src="js/modernizr.custom.63321.js"></script>

		<!--<script src="js/cherry.js" type="text/javascript"></script>-->

	</head>
	<body>
		<div class="container">
			<header>
				<h1>Welcome to <strong>2Bet</strong> auctions</h1>
				<h2>Your online realtime betting service</h2>
			</header>
		</div>
		<div class="nets_messge">
			<?php 
				if($_SESSION['nets_message']) {
					echo $_SESSION['nets_message'];
					$_SESSION['nets_message'] = NULL;
				}
			?>
		</div>
		<div id='content'>
			<div id="auth_div">
			<?php 
				if(isset($_SESSION['userid'])){
				$coins = $bet->get_wallet($_SESSION['local_userid']);
			?>
			Welcome: <strong><?php echo $_SESSION['username']; ?></strong> | Coins available: <strong></string><span id="usercoins_<?php echo $_SESSION['local_userid']; ?>"><?php echo $coins; ?></span></strong> | 
			<a href="login-logout.php" title="Log user <?php echo $_SESSION['username'] ?> out"><strong>Sign out</strong></a>
			<?php } else { ?>
				<a href="login.php" title="Sign in"><strong>Sign in</strong></a>
			<?php } ?>
			</div>
			<hr >
			<?php
			$bid = $bet->get_bids();
			echo '<div id="bids-content">';
				while ($row = $bid -> fetch_object()) {
					$username = ($row->username == NULL) ? 'No bids' : $row->username;
					$coins = ($row->sum == NULL) ? 0 : $row->sum;
					echo '<div id="2bid_'.$row->product_id.'">';
					echo '<h2>'.$row -> name.'</h2>';
					echo '<div class="img"><img src="assets/products/image_'.$row->product_id.'.png"></div>';
					echo '<span class="bidder">'.$username.'</span>';
					echo '<input type="hidden" name="product_id" class="productid" value="'.$row->product_id.'">';
					echo $button;
					echo '<p class="value">'.$coins.'</p>';
					echo '</div>';
				}
			echo '</div>';
			echo '<div class="clearB"></div>';
			?>
			<br>
			<?php
			echo '<div class="clearB"></div>';
			
			echo '<strong>Bids history</strong>';
			echo '<p id="bidhistory"></p>';
			?>
			<br>
			<div class="link">
				<a href="payment.php"><strong>Buy coins to spend!</strong></a>
			</div>
			<div class="link">
				<a href="tutorial.php"><strong>Watch a tutorial here</strong></a>
			</div>
		</div>

		<script>
			var source = new EventSource("application/controller/bids.php");
			source.onmessage = function(e) {
				
				var data = JSON.parse(e.data);
				//var history = JSON.parse(e.history);
				
				$.each(data, function(i,value){
					var th = $('#2bid_'+value.id),
						current = th.find('.value');
					
					if(current.text() != value.value && value.value != undefined){
						th.find('.value').html(value.value).effect("highlight", {}, 1500);
						th.find('.bidder').html(value.username);
						
							// If last userid is not equal to your session "userid" then value.button return 1, else 0.
							if(value.button === 1){
								$(th).find('.bidnow').removeAttr("disabled");
							}
							$('#bidhistory').prepend('A bid has been made by <strong>' + value.username + '</strong> on <strong> productID ' + value.id + '</strong> with amount: ' + value.value + ' at ' + value.timestamp + "<br>");
					}
				});
			};
			$('.bidnow').click(function(event){
				event.preventDefault();
				var th = $(this);
				$.post('application/controller/bids.php',{product_id: $(this).parent().find('.productid').val()}).done(function(data) {
					th.attr("disabled", "disabled");
					var data = JSON.parse(data);
										
					$('#usercoins_'+data.user_id).html(data.amount);
					
				});
			});
			$('#form_deposit').submit(function(event){
				event.preventDefault();
				var th = $(this);
				$.post('application/controller/nets.php',{amount: $('#deposit_amount').val(),cardnumber: $('#deposit_cardnumber').val(),product_id: $('#deposit_security').val()}).done(function(data) {
					console.log(data);
				});
			});
		</script>
	</body>
</html>